package cn.smile.business.basic.impl;

import cn.smile.bean.entity.basic.UserShop;
import cn.smile.bean.form.basic.shop.SavePositionForm;
import cn.smile.business.basic.IUserShopService;
import cn.smile.commons.constant.NumberConstant;
import cn.smile.commons.domain.BaseServiceImpl;
import cn.smile.commons.exceptions.BusinessException;
import cn.smile.commons.response.MyResponseCode;
import cn.smile.repository.basic.UserShopMapper;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserShopServiceImpl extends BaseServiceImpl<UserShopMapper, UserShop> implements IUserShopService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean bindShop(String userId, String shopCode, Integer dicValue) {
        log.info("[UserShopServiceImpl].[bindShop] ------> 用户绑定店铺 Start, userId = {}, shopCode = {}, dicValue = {}", userId, shopCode, dicValue);
        List<UserShop> shopList = baseMapper.selectList(new QueryWrapper<UserShop>().lambda().eq(UserShop::getDeleted, Boolean.FALSE).eq(UserShop::getUserId, userId));
        if (!CollectionUtils.isEmpty(shopList)) {
            //用户绑定过其他店铺 全部移除
            for (UserShop entity : shopList) {
                super.delBaseCol(entity);
                baseMapper.updateById(entity);
            }
        }
        UserShop entity = new UserShop();
        super.addBaseCol(entity);
        entity.setUserId(userId);
        entity.setShopCode(shopCode);
        entity.setDicValue(dicValue);
        int insertNum = baseMapper.insert(entity);
        log.info("[UserShopServiceImpl].[bindShop] ------> 用户绑定店铺 End, insertNum = {}", insertNum);
        return insertNum > NumberConstant.ZERO;
    }

    @Override
    public String getMyShopCode(String userId) {
        log.info("[UserShopServiceImpl].[getMyShopCode] ------> 查询用户所属店铺Code Start, userId = {}", userId);
        UserShop userToShop = baseMapper.selectOne(new QueryWrapper<UserShop>().lambda().eq(UserShop::getUserId, userId).eq(UserShop::getDeleted, Boolean.FALSE));
        String code = ObjectUtils.isEmpty(userToShop) ? null : userToShop.getShopCode();
        log.info("[UserShopServiceImpl].[getMyShopCode] ------> 查询用户所属店铺Code End, code = {}", code);
        return code;
    }

    @Override
    public Map<String, Integer> getUserIdsByCode(String shopCode) {
        log.info("[UserShopServiceImpl].[getUserIdsByCode] ------> 查询店铺用户ID集合 Start, shopCode = {}", shopCode);
        List<UserShop> userShopList = baseMapper.selectList(new QueryWrapper<UserShop>().lambda().eq(UserShop::getShopCode, shopCode).eq(UserShop::getDeleted, Boolean.FALSE));
        Map<String, Integer> userDicMap = Maps.newHashMap();
        if (!CollectionUtils.isEmpty(userShopList)) {
            userShopList.forEach(entity -> userDicMap.put(entity.getUserId(), entity.getDicValue()));
        }
        log.info("[UserShopServiceImpl].[getUserIdsByCode] ------> 查询店铺用户ID集合 End, userIds.size() = {}", userDicMap.size());
        return userDicMap;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean savePosition(SavePositionForm form) {
        log.info("[UserShopServiceImpl].[savePosition] ------> 指定用户保存新职位信息 Start, form = {}", JSON.toJSONString(form));
        UserShop entity = baseMapper.selectOne(new QueryWrapper<UserShop>().lambda().eq(UserShop::getUserId, form.getUserId()).eq(UserShop::getDeleted, Boolean.FALSE));
        entity.setDicValue(Integer.valueOf(form.getPositionId()));
        super.updateBaseCol(entity);
        int updateNum = baseMapper.updateById(entity);
        log.info("[UserShopServiceImpl].[savePosition] ------> 指定用户保存新职位信息 End, updateNum = {}", updateNum);
        return updateNum > NumberConstant.ZERO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean relieveBind(String userId) {
        log.info("[UserShopServiceImpl].[relieveBind] ------> 解除用户店铺绑定信息 Start, userId = {}", userId);
        if (ObjectUtils.isEmpty(userId)) {
            throw new BusinessException(MyResponseCode.USER_ID_NULL_ERROR);
        }
        UserShop entity = baseMapper.selectOne(new QueryWrapper<UserShop>().lambda().eq(UserShop::getUserId, userId).eq(UserShop::getDeleted, Boolean.FALSE));
        super.delBaseCol(entity);
        int updateNum = baseMapper.updateById(entity);
        log.info("[UserShopServiceImpl].[relieveBind] ------> 解除用户店铺绑定信息 End, updateNum = {}", updateNum);
        return updateNum > NumberConstant.ZERO;
    }
}
